<template>
    <div class="main">
        <!--  商品分类 -->
        <el-card class="box-card">
            <el-form :inline="true" :model="page" class="demo-form-inline">
                <el-form-item label="分类名称">
                    <el-input v-model="page.classificationName" placeholder="请输入" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">搜索</el-button>
                    <el-button type="primary" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
            <el-button type="primary" style="margin-bottom: 30px;" @click="addEdiClick(1)">新增</el-button>

            <el-table :data="tableData" style="width: 100%" max-height="600px" row-key="id"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
                <el-table-column prop="classification_name" label="分类名称" show-overflow-tooltip width="300"/>
                <el-table-column label="封面图" align="center" width="200">
                    <template #default="scope">
                        <img v-if="scope.row.classification_photo" style="width: 100px;height: 100px;"
                            :src="scope.row.classification_photo" alt="">
                    </template>
                </el-table-column>
                <el-table-column prop="operating_time" label="创建时间" align="center" />
                <el-table-column fixed="right" label="操作" width="160">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="addEdiClick(2, scope.row)">编辑</el-button>
                        <el-button v-if="scope.row.parent_id == ''" link type="primary" size="small"
                            @click="addEdiClick(3, scope.row)">添加下级</el-button>

                        <el-button link type="danger" size="small" @click="delData(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination v-if="total != 0" :page="page" :total="total" @pageSizeChanged="handlePageSizeChange"
                @currentPageChanged="handleCurrentPageChange" />
        </el-card>
    </div>
    <!-- 新增修改 -->
    <el-dialog v-model="dialogVisible" :title="isAdd ? '新增' : '修改'" width="500px" top="15vh"
        :before-close="handleClose">
        <Add v-if="dialogVisible" @closeOK="closeOK" @closeAdd="handleClose" :isAdd="isAdd" :isAddXJ="isAddXJ"
            :row="rowData.row" />
    </el-dialog>
    <el-dialog v-model="isAddXJShow" title="新增下级" width="500px" top="15vh" :before-close="handleCloseisAddXJ">
        <el-form ref="ruleFormRefisAddXJ" :model="ruleFormisAddXJ" label-width="120px" size="default" status-icon
            :rules="rules">
            <el-form-item label="下级分类名称" prop="classificationName">
                <el-input v-model="ruleFormisAddXJ.classificationName" placeholder="请输入" />
            </el-form-item>
             <el-form-item>
                <el-button type="primary" @click="OKisAddXJ(ruleFormRefisAddXJ)">
                    确认
                </el-button>
                <el-button @click="handleCloseisAddXJ">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script setup>
import Add from './add.vue' // 引入组件
import { classificationList, classificationDelete, classificationAdd } from '@/api/goods.js'
import { onMounted, reactive, ref } from 'vue'
import Pagination from '@/components/pagination/index.vue' // 引入分页组件
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRoute } from 'vue-router';
const route = useRoute();
const dialogVisible = ref(false)
const page = reactive({
    classificationName: "",
    pageNum: 1,
    pageSize: 10
})
const total = ref(0)
const tableData = reactive([])
const isAdd = ref(false)
const isAddXJ = ref(false)
const isAddXJShow = ref(false)
const rowData = reactive({
    row: {}
})
// 搜索
const onSubmit = () => {
    getList()
}
// 重置
const reset = () => {
    page.classificationName = "";
    page.pageNum = 1;
    page.pageSize = 10;
    getList()
}
// 分页
const handlePageSizeChange = (val) => {
    page.pageNum = 1
    page.pageSize = val
    getList()
}

const handleCurrentPageChange = (val) => {
    page.pageNum = val
    getList()
}
// 确认弹框
const closeOK = () => {
    dialogVisible.value = false;
    getList()
}
// 删除
const delData = (scope) => {
    ElMessageBox.confirm(
        '是否删除该条数据?',
        '删除',
        {
            confirmButtonText: 'OK',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            classificationDelete(scope.id).then(res => {
                if (res.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getList()
                }
            })

        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}
const ruleFormRefisAddXJ = ref()
const ruleFormisAddXJ = reactive({
    classificationName: '',
    parentId: ''
})
const rules = reactive({
    classificationName: [
        { required: true, message: '请输入', trigger: 'blur' },
    ],
})

// 弹框确认
const OKisAddXJ = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            classificationAdd(ruleFormisAddXJ).then(res => {
                if (res.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: '添加成功',
                    })
                    isAddXJShow.value = false
                    getList()
                }
            })

        } else {
            console.log('error submit!', fields)
        }
    })

}
// 弹框关闭
const handleCloseisAddXJ = () => {
    isAddXJShow.value = false
}
// 弹框关闭
const handleClose = () => {
    dialogVisible.value = false
}
// 新增
const addEdiClick = (num, row) => {
    if (num == 2) {
        dialogVisible.value = true
        isAdd.value = false
        rowData.row = row
        if (row.parent_id == '') {
            isAddXJ.value = true
        } else {
            isAddXJ.value = false
        }

    } else if (num == 3) {
        isAddXJShow.value = true
        ruleFormisAddXJ.parentId = row.id
        ruleFormisAddXJ.classificationName = ''
    } else {
        dialogVisible.value = true
        isAdd.value = true
        isAddXJ.value = true
    }

}

onMounted(() => {

    getList()
})
// 获取列表
const getList = () => {
    tableData.length = 0
    total.value = 0
    classificationList(page).then(res => {
        if (res.code == 200) {
            tableData.push(...res.data.records)
            total.value = res.data.total
        }
    })
}
</script>

<style lang="scss" scoped>
.box-card :deep(.el-select__wrapper) {
    width: 100px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

.main {
    padding: 0 5px 5px 5px;
}
</style>